﻿using System;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace ImpExpData
{
    class ImportColumnsZero:ImportData
    {
        public string[] Columns { get; set; }
        public string ColumnByZero { get; set; }
        
        protected override void InsertData(System.Data.SqlServerCe.SqlCeResultSet rs, System.Xml.XmlReader xmlReader)
        {
            SqlCeUpdatableRecord rec = rs.CreateRecord();
            for (int i = 0; i < Columns.Length; i++)
            {
                rec.SetValue(i, xmlReader.GetAttribute(Columns[i]));
            }
            rs.Insert(rec);
        }

        protected override void UpdateData(System.Data.SqlServerCe.SqlCeResultSet rs, System.Xml.XmlReader xmlReader)
        {
            bool isUpdate = false;

            for (int i = 1; i < Columns.Length; i++)
            {
                if (xmlReader.GetAttribute(Columns[i]) != null &&
                    rs[i].ToString() != xmlReader.GetAttribute(Columns[i]))
                {
                    rs.SetValue(i, xmlReader.GetAttribute(Columns[i]));
                    isUpdate = true;
                }
            }
            if (isUpdate)
            {
                rs.Update();
            }
        }

        protected override void DeleteData(System.Data.SqlServerCe.SqlCeResultSet rs, System.Xml.XmlReader xmlReader)
        {
            int i = rs.GetOrdinal(ColumnByZero);
            rs.SetValue(i, 0);
            rs.Update();
        }
    }
}
